#include "notetask.h"
#include "ui_notetask.h"
#include "globalPara.h"
NoteTask::NoteTask(QWidget *parent) :
    QFrame(parent),
    ui(new Ui::NoteTask)
{
    ui->setupUi(this);

}

NoteTask::NoteTask(qint32 _screenWidth, qint32 _screenHeight,NoteItemMap * _pItemMap)
{
    ui->setupUi(this);

    init(_screenWidth,_screenHeight);
    setLayout(_pVLayout);
    update(_pItemMap);
}

NoteTask::~NoteTask()
{
    delete ui;
}

void NoteTask::init(qint32 _screenWidth, qint32 _screenHeight)
{
    //QString _styleBackgroud("background-color:rgb(100,100,128,255)");
    QString _styleBackgroud("background-color:rgb(128,128,128,255)");
    setStyleSheet(_styleBackgroud);
    setWindowFlags(Qt::FramelessWindowHint);

    _size.setWidth(gNoteTaskWidth);
    _size.setHeight(_screenHeight);
    _pos.setX(_screenWidth - _size.width());
    _pos.setY(0);
    move(_pos);
    resize(_size);

    _pVLayout_Note = new QVBoxLayout;//conserve notes
    _pVLayout_Note->setAlignment(Qt::AlignTop);

    _pVLayout_Task = new QVBoxLayout;//conserve tasks
    _pVLayout_Task->setAlignment(Qt::AlignTop);

    _pVLayout = new QVBoxLayout;//conserve note and task Layouts
    _pVLayout->setAlignment(Qt::AlignTop);
    _pVLayout->setGeometry(QRect(_pos,_size));//set gridlayout position and size
    _pVLayout->addLayout(_pVLayout_Note);
    _pVLayout->addLayout(_pVLayout_Task);
}

void NoteTask::initEachItemUI(NoteType type,QString dateTitle,qint32 idx,qint32 progress)
{
    QLabel *label = new QLabel;
    QPalette pal = label->palette();
    pal.setColor(QPalette::WindowText,QColor(255,0,0));
    label->setPalette(pal);//set font color

    label->setText(dateTitle);//set note text
    _labels.insert(idx,label);

    if(NOTE==type)
    {
         _pVLayout_Note->addWidget(label);
    }

    //***************Customizing ProgressBar*******************************

    else if(TASK==type)
    {
        QProgressBar *bar = new QProgressBar;
        bar->setStyleSheet( "QProgressBar {border: 2px solid grey; border-radius: 5px; }");
        bar->setStyleSheet( "QProgressBar::chunk {background-color: #CD96CD; width: 20px;}");
        bar->setAutoFillBackground(true);
        bar->setMinimum(0);
        bar->setMaximum(100);
        bar->setValue(progress);
        _progBar.insert(idx,bar);
        _pVLayout_Task->addWidget(label);
        _pVLayout_Task->addWidget(bar);
    }
}

void NoteTask::setWidth(qint32 _width)
{
    _size.setWidth(_width);
}

void NoteTask::setHeight(qint32 _height)
{
    _size.setWidth(_height);
}

void NoteTask::setPosition(QPoint position)
{
    _pos = position;
}

void NoteTask::setSize(QSize size)
{
    _size = size;
}

void NoteTask::update(NoteItemMap * pItemMap)
{
    updateData(pItemMap);
    updateUI();
}

//get new data from outside
void NoteTask::updateData(NoteItemMap * pItemMap)
{
    _allValid_Note.clear();
    _allValid_Task.clear();
    QMap<QString,NoteItemOneDay>::const_iterator iterMap = pItemMap->constBegin();
    for(;iterMap!=pItemMap->constEnd();++iterMap)
    {//save each itemOneDay
        //QDate _date = QDate::fromString(iterMap.key());
        NoteItemOneDay _itemOneDayTemp = iterMap.value();
        qint32 _count = _itemOneDayTemp.getSize();

        QMap<QString,NoteItem>::const_iterator iterOneday = _itemOneDayTemp.constBegin();
        for(qint32 idx=0;idx<_count;++idx,++iterOneday)
        {//there are _count items in current day
            NoteItem _item = iterOneday.value();

            QString key = _item.getDate().toString(DATE_FORMAT);
            key += _item.getTime().toString(DATE_FORMAT);

            if(NOTE == _item.getNoteType())
                _allValid_Note.addItem(key,_item);
            else if(TASK == _item.getNoteType())
                _allValid_Task.addItem(key,_item);
        }
    }
}

void NoteTask::updateUI()
{
    clearLayout();

    QMap<QString,NoteItem>::const_iterator iterNote = _allValid_Note.constBegin();
    QMap<QString,NoteItem>::const_iterator iterTask = _allValid_Task.constBegin();

    for(qint32 idxNote=0 ;iterNote != _allValid_Note.constEnd();++iterNote,++idxNote)
    {//update Notes
        NoteItem _itemTemp = iterNote.value();
        QString _strTemp = _itemTemp.getDate().toString(DATE_FORMAT);
        _strTemp = _strTemp + '\t' +_itemTemp.getTime().toString(DATE_FORMAT) + '\n';
        _strTemp += _itemTemp.getTitle();
        initEachItemUI(NOTE,_strTemp,idxNote);
    }

    for(qint32 idxTask=0 ;iterTask != _allValid_Task.constEnd();++iterTask,++idxTask)
    {//update Tasks
        NoteItem _itemTempTask = iterTask.value();
        QString _strTempTask = _itemTempTask.getDate().toString(DATE_FORMAT);
        _strTempTask = _strTempTask + '\t' +_itemTempTask.getTime().toString(DATE_FORMAT) + '\n';
        _strTempTask += _itemTempTask.getTitle();
        initEachItemUI(TASK,_strTempTask,idxTask,_itemTempTask.getProgress());
    }
    qDebug()<<"Valid Task Count-"<<_allValid_Task.getSize();

}

void NoteTask::clearLayout()
{// remove all layouts in NoteTask

    _labels.clear();
    _progBar.clear();

    if(_pVLayout_Note)
        delete _pVLayout_Note;
    if(_pVLayout_Task)
        delete _pVLayout_Task;

    _pVLayout_Note = new QVBoxLayout;//conserve notes
    _pVLayout_Note->setAlignment(Qt::AlignTop);

    _pVLayout_Task = new QVBoxLayout;//conserve tasks
    _pVLayout_Task->setAlignment(Qt::AlignTop);

    _pVLayout->addLayout(_pVLayout_Note);
    _pVLayout->addLayout(_pVLayout_Task);

}
